layui.use(['element', 'table', 'laytpl'], function () {
    var $ = layui.$,
        table = layui.table,
        layer = layui.layer;
    var tableIns = table.render({
        elem: '#demo'
        , id: "mytable"
        , url: '/building/list' //数据接口
        , page: true //开启分页
        , limit: 15
        , limits: [15, 30, 60, 100]
        , response: {
            statusName: 'code' //数据状态的字段名称，默认：code
            , statusCode: 200 //成功的状态码，默认：0
            , msgName: 'msg' //状态信息的字段名称，默认：msg
            , countName: 'total' //数据总数的字段名称，默认：count
            , dataName: 'rows' //数据列表的字段名称，默认：data
        }
        , cols: [[ //表头
            {type: 'checkbox'}
            , {title: '序号', type: 'numbers'}
            , {field: 'name', title: '楼宇名称'}
            , {field: 'floors', title: '楼层数'}
            , {field: 'roomNum', title: '房间数'}
            , {
                field: 'texture', title: '建筑材质', templet: function (d) {
                    switch (d.texture) {
                        case 1:
                            return '钢架结构';
                        case 2:
                            return '砖混结构';
                        case 3:
                            return '简易房';
                        case 4:
                            return '土木机构';
                        case 5:
                            return '彩钢板';
                        case 6:
                            return '其它';
                        default:
                            return '';
                    }
                }
            }
            , {
                field: 'area', title: '建筑面积', templet: function (d) {
                    return d.area + "m<sup>2</sup>"
                }
            }
            , {
                field: 'property', title: '建设属性', templet: function (d) {
                    switch (d.property) {
                        case 1:
                            return '新建';
                        case 2:
                            return '改建';
                        case 3:
                            return '扩建';
                        case 4:
                            return '租用';
                        default:
                            return '';
                    }
                }
            }
            , {title: '操作', toolbar: "#toolbar", width: "18%"}

        ]]
    });

    //工具条
    table.on("tool(test)", function (obj) {
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        if ("edit" == layEvent) {
            location.href = "/building/edit?id=" + data.id;
        } else if ("delete" == layEvent) {
            var params = "buildingId=" + data.id;
            layer.open({
                title: '提示',
                type: 1,
                content: "删除该楼宇则会删除对应的房间与床位，您确认删除该楼宇吗？",
                area: ['400px', '150px'],
                btn: ['确认'],
                yes: function (index, layero) {
                    $.ajax({
                        type: 'post',
                        url: '/bed/selecBedtList',
                        dataType: 'json',
                        data: params,
                        success: function (result) {
                            if (result.code == 0) {
                                layer.msg("该楼住有老人，不能删除。");
                            } else {
                                location.href = "/building/delete?id=" + data.id;
                            }
                        }
                    });
                }
            });

        } else if ("room" == layEvent) {
            var htmlstr =
                '<form class="layui-form" id="openForm">' +
                '  <table class="layui-table">\n' +
                '                        <colgroup>\n' +
                '                            <col width="150">\n' +
                '                            <col width="150">\n' +
                '                            <col width="200">\n' +
                '                        </colgroup>\n' +
                '                        <thead>\n' +
                '                        <tr>\n' +
                '                            <th>楼宇</th>\n' +
                '                            <th>楼层</th>\n' +
                '                            <th>房间数</th>\n' +
                '                            <th>操作</th>\n' +
                '                        </tr>\n' +
                '                        </thead>\n' +
                '                        <tbody>\n';
            var htmlend =
                '                        </tbody>\n' +
                '                    </table>' +
                '</form>';
            var htmktr = ''
            var builNum = obj.data.floors;//层数
            var buildingId = obj.data.id;//楼宇id
            for (var i = 1; i < Number(builNum) + 1; i++) {
                htmktr +=
                    '<tr>\n' +
                    '                            <td><input type="text"  readonly autocomplete="off" class="layui-input" value="' + data.name + '"></td>\n' +
                    '                            <td><input type="text" name="floor" readonly autocomplete="off" class="layui-input" value="' + i + '"></td>\n' +
                    '                            <td> <input type="number" name="roomNum" oninput="if(value.length>3)value=value.slice(0,3)" max="300" onkeyup="value=value.replace(/^(0+)|[^\\d]+/g,\'\')" lay-verify="required" placeholder="请输入房间数" autocomplete="off" class="layui-input"></td>\n' +
                    '                            <td> <a href="javascript:void(0)"  onclick="javascript:parent.delRow(this)" class="layui-btn layui-btn-danger layui-btn-xs" >删除</a></td>\n' +
                    ' </tr>';

            }
            var html = htmlstr + htmktr + htmlend;
            var params = "budid=" + data.id;
            $.ajax({
                type: 'post',
                url: '/room/selectRooms',
                dataType: 'json',
                data: params,
                success: function (result) {
                    if (result.data.length > 0) {
                        layer.open({
                            title: '提示',
                            type: 1,
                            content: "<div>该楼宇已快速生成过房间，不能再次生成，如需添加房间请前往房间管理点击新增按钮<a href='/room/add' style='color: blue'>>>>>>前往房间新增</a></div>",
                            area: ['400px', '150px']
                        });
                        return false;
                    } else {
                        layer.open({
                            title: '房间管理'
                            , area: ['500px', '500px']
                            , btn: ['生成', '取消']
                            , content: html
                            , type: 1
                            , yes: function (index, layero) {
                                //按钮【生成】的回调
                                var params = $("#openForm").serialize();
                                params += "&buildingId=" + buildingId;
                                $.ajax({
                                    type: 'post',
                                    url: '/room/saveList',
                                    dataType: 'json',
                                    data: params,
                                    success: function (result) {
                                        //正确处理
                                        if (result.code == 200) {
                                            layer.msg("创建房间成功", {
                                                time: 1000
                                            }, function () {
                                                layer.close(1);
                                                tableIns.reload();
                                            });
                                        } else if (result.code == 0) {
                                            layer.msg(result.message);
                                        } else {
                                            layer.msg("服务器异常,请重试");
                                        }
                                    }
                                });
                                layer.close(index);
                            }
                        });
                    }
                }
            });

        }
    });


    delRow = function (that) {
        $(that).closest("tr").remove();
    }

});